package top.callname.service.impl;

import top.callname.bean.AdminBean;
import top.callname.bean.AdminPageList;
import top.callname.bean.ConstanstBean;
import top.callname.bean.RspInfoBean;
import top.callname.dao.AdminDao;
import top.callname.service.AdminService;
import top.callname.service.ClassService;
import top.callname.util.BaseUtil;

public class AdminServiceImpl implements AdminService {

	private AdminDao adminDao = new AdminDao();
	private ClassService classService = new ClassServiceImpl();

	@Override
	public AdminBean adminLogin(String username, String passwd) {
		AdminBean bean = new AdminBean();
		if (!username.contains("\"") && !username.contains("'")
				&& !username.contains(" ") && !passwd.contains("\"")
				&& !passwd.contains("'") && !passwd.contains(" ")) {
			AdminBean rspBean = adminDao.selectAdminByPwdAndUname(username,
					passwd);
			if (rspBean != null) {
				bean = rspBean;
			} else {
				bean.setRspCode(ConstanstBean.RSP_FAULT);
				bean.setRspDesc("登录失败 : 用户名或密码错误！");
			}
		} else {
			bean.setRspCode(ConstanstBean.RSP_FAULT);
			bean.setRspDesc("登录失败 : 用户名或密码不得包含特殊字符！");
		}
		return bean;
	}

	@Override
	public AdminBean adminRegist(AdminBean adminBean) {

		adminBean.setFullname(BaseUtil.getUtf8Str(adminBean.getFullname()));

		if (adminBean.getUsername() == null
				|| "".equals(adminBean.getUsername())) {
			adminBean.setRspCode(ConstanstBean.RSP_FAULT);
			adminBean.setRspDesc("注册失败 : 用户名不得为空！");
			return adminBean;
		}
		if (adminBean.getFullname() == null
				|| "".equals(adminBean.getFullname())) {
			adminBean.setRspCode(ConstanstBean.RSP_FAULT);
			adminBean.setRspDesc("注册失败 : 姓名不得为空！");
			return adminBean;
		}
		if (adminBean.getPasswd() == null || "".equals(adminBean.getPasswd())
				|| adminBean.getPasswd().length() < 6) {
			adminBean.setRspCode(ConstanstBean.RSP_FAULT);
			adminBean.setRspDesc("注册失败 : 密码必须大于6位数！");
			return adminBean;
		}
		if (!adminBean.getPasswd().equals(adminBean.getPasswd2())) {
			adminBean.setRspCode(ConstanstBean.RSP_FAULT);
			adminBean.setRspDesc("注册失败 : 两次输入的密码不一致！");
			return adminBean;
		}

		// 判断用户名是否存在
		AdminBean rsp = adminDao.selectUname(adminBean.getUsername());
		if (rsp != null) {
			adminBean.setRspCode(ConstanstBean.RSP_FAULT);
			adminBean.setRspDesc("注册失败 : 用户名已存在，请重新填写！");
			return adminBean;
		}

		boolean f = adminDao.adminRegister(adminBean);
		if (!f) {
			adminBean.setRspCode(ConstanstBean.RSP_FAULT);
			adminBean.setRspDesc("注册失败！");
		} else {
			adminBean.setRspCode(ConstanstBean.RSP_SUCCESS);
			adminBean.setRspDesc("注册成功 : 请重新登录！");
		}
		return adminBean;
	}

	@Override
	public AdminPageList getAdminByPage(int pageNow) {
		return adminDao.selectByPage(pageNow);
	}

	@Override
	public RspInfoBean deleteTeacher(Integer pid) {
		RspInfoBean rsp = new RspInfoBean();
		adminDao.deleteFromAdminClassByPid(pid);
		if(adminDao.deleteFromAdminByPid(pid)){
			rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
			rsp.setRspDesc("教师用户成功失败！");
		}else{
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("教师用户失败！");
		}
		return rsp;
	}

	@Override
	public RspInfoBean updateUserInfo(AdminBean adminBean) {
		RspInfoBean rsp = new RspInfoBean();
		adminBean.setFullname(BaseUtil.getUtf8Str(adminBean.getFullname()));
		if (adminBean.getPid() == null) {
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("修改失败 : 用户PID不得为空！");
			return rsp;
		}
		if (adminBean.getUsername() == null
				|| "".equals(adminBean.getUsername())) {
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("修改失败 : 用户名不得为空！");
			return rsp;
		}
		if (adminBean.getFullname() == null
				|| "".equals(adminBean.getFullname())) {
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("修改失败 : 姓名不得为空！");
			return rsp;
		}
		if (adminBean.getPasswd() == null || "".equals(adminBean.getPasswd())
				|| adminBean.getPasswd().length() < 6) {
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("注册失败 : 密码必须大于6位数！");
			return rsp;
		}
		if(adminDao.updateByPid(adminBean)){
			rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
			rsp.setRspDesc("修改成功："+adminBean.toString());
		}else{
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("修改失败 ！");
		}
		return rsp;
	}

	@Override
	public AdminBean getAdminByPid(Integer pid) {
		AdminBean admin = adminDao.selectByPid(pid);
		admin.setClassList(classService.getList(admin.getPid()));
		return admin;
	}

	@Override
	public RspInfoBean updateAdminClass(Integer pid, String[] cids) {
		RspInfoBean rsp  = new RspInfoBean();
		if(cids!=null && cids.length>0){
			adminDao.deleteFromAdminClassByPid(pid);
			for (int i = 0; i < cids.length; i++) {
				adminDao.insertAdminClass(pid, cids[i]);
			}
			rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
			rsp.setRspDesc("修改成功："+rsp.toString());
		}else{
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("修改失败 ！");
		}
		return rsp;
	}

}
